<?php

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * The PHPCodeProvider instance
 */

$provider = new PHPCodeProvider();

/*
 * The iteration variable
 */

$i = new VariableReferenceExpression('i');

/*
 * Initialize the variable
 */

$init3 = new AssignStatement($i, new PrimitiveExpression(1));

/*
 * Create the while statement
 */

$while1 = new WhileStatement();

$while1->TestExpression = new BinaryOperatorExpression(
    BinaryOperator::LESS_THAN_OR_EQUAL, 
    $i, new PrimitiveExpression(10));

$while1->Statements->add(new SnippetStatement('    echo $i++'));

/*
 * Generate the code.
 */

$provider->generateCodeFromStatement($init3, $writer, $options);
$provider->generateCodeFromStatement($while1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * a simple do..while loop
 * It executes only once
 */

$doWhile1 = new WhileStatement();
$doWhile1->isDoWhile = true;

$doWhile1->TestExpression = new BinaryOperatorExpression(
    BinaryOperator::GREATER_THAN, 
    $i, new PrimitiveExpression(1));

$doWhile1->Statements->add(new SnippetStatement('    echo $i'));

$provider->generateCodeFromStatement($init3, $writer, $options);
$provider->generateCodeFromStatement($doWhile1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be:
 * 
 * $i = 1;
 * while ($i <= 10) 
 * {
 *    echo $i++;
 * }
 * 
 * $i = 1;
 * do 
 * {
 *     echo $i;
 * } while ($i > 1);
 * 
 */

?>
